//
//  Created by  fred on 2017/1/12.
//  Copyright © 2016年 Alibaba. All rights reserved.
//

package top.zywork.common;

import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;

import java.io.IOException;

public class HttpsApiClient extends ApacheHttpClient{
//    public final static String HOST = "域名";
    static HttpsApiClient instance = new HttpsApiClient();
    public static HttpsApiClient getInstance(){return instance;}

    public void init(HttpClientBuilderParams httpClientBuilderParams){
        httpClientBuilderParams.setScheme(Scheme.HTTPS);
//        httpClientBuilderParams.setHost(HOST);
        super.init(httpClientBuilderParams);
    }



    public void BankCardVerification(String path,String name , String idcard , String bankcard , String mobile , ApiCallback callback) {
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("name" , name , ParamPosition.QUERY , true);
        request.addParam("idcard" , idcard , ParamPosition.QUERY , false);
        request.addParam("bankcard" , bankcard , ParamPosition.QUERY , true);
        request.addParam("mobile" , mobile , ParamPosition.QUERY , false);

        sendAsyncRequest(request , callback);
    }

    /**
     * @Author: 95DBC
     * @Date: 2020/11/2 17:56
     * @param name   开户人的真实姓名
     * @param idcard   身份证号码
     * @param bankcard  银行卡号
     * @param mobile    银行预留手机号
     * @param path    路径
     * @Description:
     *
     */

    public ApiResponse SyncMode(String path,String name , String idcard , String bankcard , String mobile) {
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("name" , name , ParamPosition.QUERY , true);
        request.addParam("idcard" , idcard , ParamPosition.QUERY , false);
        request.addParam("bankcard" , bankcard , ParamPosition.QUERY , true);
        request.addParam("mobile" , mobile , ParamPosition.QUERY , false);



        return sendSyncRequest(request);
    }

    public static String getResultString(ApiResponse response) throws IOException {
        StringBuilder result = new StringBuilder();
        result.append("").append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        result.append("ResultCode:").append(SdkConstant.CLOUDAPI_LF).append(response.getCode()).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        if(response.getCode() != 200){
            result.append("Error description:").append(response.getHeaders().get("X-Ca-Error-Message")).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        }

        result.append("ResultBody:").append(SdkConstant.CLOUDAPI_LF).append(new String(response.getBody() , SdkConstant.CLOUDAPI_ENCODING));

        return result.toString();
    }

}